package com.ja.openchat.ui.base;

import android.view.View;
import android.widget.TextView;

import androidx.databinding.ViewDataBinding;

import com.ja.openchat.R;
import com.zy.openchat.core.manager.TgEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseViewModel;


public abstract class JaBaseActivity<V extends ViewDataBinding, VM extends BaseViewModel> extends BaseActivity {
    Unbinder unbinder;

    protected boolean isHaveButterKnife() {
        return false;
    }

    protected boolean isHaveEventBus() {
        return false;
    }
    @Override
    public void initView() {
        super.initView();
        //注册事件
        if (isHaveEventBus()) {
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
        }
        if (isHaveButterKnife())
            unbinder = ButterKnife.bind(this);
        doButtonClick();
    }
    public void setTitle(String title) {
        TextView titleText = findViewById(R.id.tv_bar_title);
        if (titleText != null) {
            titleText.setText(title);
        }
    }
    protected boolean isHaveBackButton() {
        return true;
    }

    private void doButtonClick() {
        View backView = findViewById(R.id.iv_bar_back);
        if (backView != null) {
            if (isHaveBackButton()) {
                backView.setVisibility(View.VISIBLE);
                backView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
            } else {
                backView.setVisibility(View.GONE);
            }
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        if (unbinder != null && unbinder != Unbinder.EMPTY) {
            unbinder.unbind();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final TgEvent tg_event) {
    }
}
